var listObj = (function ($) {
	var sourceData,postData;
	var source ='' // N:请假申请 H护士长请假
	var pageType = '',imgSrc="",filterTypes="",typeId
	var start = 0,Limit = 20,wardID=""
	var pullDom;
	var pulling = false;
	var perInfo,method,deleteMethod
	$.init();
	$.plusReady(function() {
		baseSetup();
	})
	if (!mui.os.plus) {
		baseSetup()
	}
	function deleteOne(rw){
		var pdict = {'id':rw}
		if (pageType == '调岗申请') {
			pdict = {'parr':rw}
		}
		var paramDict = {'method':deleteMethod,'params':JSON.stringify(pdict),'type':'Method'}
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			if (result.msg != '1') {
				mui.toast('删除失败')
				return
			}
			resetRefresh()
		}, function(errorStr) {
		 	
		});
	}
	
	function getApplyData(){
		if (pulling) {
			pullDom.endPullUpToRefresh()
			return;
		}
		pulling = true
		//病区id^状态^开始日期^结束日期
		var stDate = $('#stBtn')[0].innerText
		var endDate = $('#endBtn')[0].innerText
		if (stDate!="" && endDate!="" && stDate > endDate) {
			mui.toast('结束时间不能早于开始时间')
			pullDom.endPullUpToRefresh()
			return;
		}
		var holidaypost = filterTypes
		var statusStr = ""
		if (typeId != undefined) {
			holidaypost = typeId
			statusStr = 'A'
		}
		var pdict = {'start':start,'limit':Limit,'parr':wardID+'^'+statusStr+'^'+stDate+'^'+endDate,'nurseid':perInfo['loginID'],'source':source,'holidaypost':holidaypost}
		if (pageType == '调岗申请') {
			//StDate|2022-06-01^EndDate|2022-06-30^Person|^Ward|29^Status|
			pdict = {parr:'StDate|'+stDate+'^EndDate|'+endDate+'^Person|'+perInfo.loginName+'^Ward|'+wardID+'^Status|','id':perInfo.loginID};
		}
		var paramDict = {'method':method,'params':JSON.stringify(pdict),'type':'RecQuery'}
		//console.log(JSON.stringify(pdict))
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			var dataArr = result.data
			if (!dataArr) {
				pullDom.endPullUpToRefresh(true);
				pulling = false
				return;
			}
			
			if (start == 0) {
				sourceData = dataArr;
			}else{
				sourceData = sourceData.concat(dataArr)
			}
			updateUI(dataArr)
			if (dataArr.length < Limit) {
				pullDom.endPullUpToRefresh(true);
			}else{
				pullDom.endPullUpToRefresh();
			}
			start += dataArr.length;
			pulling = false
		}, function(errorStr) {
		 	pullDom.endPullUpToRefresh();
		 	pulling = false
		});
	}
	function updateUI(dataArr){
		var UL = $('#UL')[0];
		if (start == 0) {
			mui('#UL li').each(function (index,element) {
				element.removeEventListener('tap',tableCellTapped)
			})
			UL.innerHTML = ''
		}
		var oldCount = UL.querySelectorAll('li').length;
		for(var i=0; i<dataArr.length; i++){
			var oneData = dataArr[i]
			var html = getAppHtml(oneData)
			
			var newLi = document.createElement('li')
			if (pageType == '调岗申请') {
				newLi.setAttribute('data-person',oneData['CreateUserPr'])
			}
			newLi.setAttribute('class','mui-table-view-cell')
			newLi.innerHTML = html
			newLi.id = 'li-'+(oldCount+i);
			if (typeId == undefined) {
				newLi.addEventListener('tap',tableCellTapped)
			}
			UL.appendChild(newLi)
		}
	}
	function tableCellTapped(e){
		var index = this.id.split('-')[1]
		var currData = sourceData[index]
		var targetStr = e.target.getAttribute('class')
		var str = 'deleteText mui-icon-trash mui-slider-right mui-btn-red deleteBox'
		if (str.indexOf(targetStr) != -1) {
			var perId = this.getAttribute('data-person')
			if (pageType == '调岗申请' && perId != perInfo.loginID) {
				mui.toast('不能删除别人创建的申请')
				return
			}
			mui.confirm('确认删除这条数据吗','删除提示',['取消','确认'],function (e) {
				if (e.index == 1) {
					var rwKey = 'rw'
					if (pageType == '调岗申请') {
						rwKey = 'RowID'
					}
					deleteOne(currData[rwKey])
				}
			})
			return
		}
		var pageName = 'pmLeaveApplyInfo'
		if (pageType == '调岗申请' && currData['TransStatus'] == '已保存') {
			pageName = 'pmTransPositionAdd'
		}
		var webview = plus.webview.create(pageName+'.html', pageName, {
			background: '#fff',
			statusbar: {
				background: '#fff'
			}
		},{'title':pageType,'sourceData':currData});
		webview.show('fade-in');
	}

	//
	function getAppHtml(dict){
		if (pageType == '调岗申请') {
			//N:已保存 Y:已提交 HZ：护士长审核  KH：科护士长审核 B：驳回 A:审核通过
			var Status = dict['TransStatus']
			var statusIcon = 'nur-shenhezhong'
			if (Status == '已保存') {
				statusIcon = 'nur-statusS'
			}else if (Status == '已提交') {
				statusIcon = 'nur-statusR'
			}else if (Status == '驳回') {
				statusIcon = 'nur-statusB'
			}else if (Status == '审核通过') {
				statusIcon = 'nur-statusA'
			}
			html = '<div class="liheader"><img class="headerImg" src="../../dependence/wkplatform/images/ic_work_user_02.png"/>'+
					'<div class="headerText mui-ellipsis">'+dealStringUndefined(dict['AppNur'])+' 调岗申请</div>'+
					'<div class="headerTime">创建日期</div></div>'+
				'<div class="statusIcon"><div class="nurfont '+statusIcon+'"></div></div>'+
				'<div class="oneline"><div class="leftText">年龄：</div><div class="rightText">'+dealStringUndefined(dict['AppNurAge'])+'</div></div>'+
				'<div class="oneline"><div class="leftText">层级：</div><div class="rightText">'+dealStringUndefined(dict['AppNurLevel'])+'</div></div>'+
				'<div class="oneline"><div class="leftText">学历：</div><div class="rightText">'+dealStringUndefined(dict['AppNurAcademic'])+'</div></div>'+
				'<div class="oneline"><div class="leftText">职称：</div><div class="rightText">'+dealStringUndefined(dict['AppDuty'])+'</div></div>'+
				'<div class="oneline"><div class="leftText">申请科室：</div><div class="rightText">'+dealStringUndefined(dict['AppWard'])+'</div></div>'+
				'<div class="oneline"><div class="leftText">申请日期：</div><div class="rightText">'+dealStringUndefined(dict['AppDate'])+'</div></div>'+
				'<div class="oneline"><div class="leftText">状态：</div><div class="rightText">'+dealStringUndefined(dict['TransStatus'])+'</div></div>'+
				'<div class="oneline"><div class="leftText">现工作岗位：</div><div class="rightText">'+dealStringUndefined(dict['AppPosition'])+'</div></div>'+
				'<div class="oneline"><div class="leftText">申请何种岗位：</div><div class="rightText">'+dealStringUndefined(dict['AppWPosition'])+'</div></div>'+
				'<div class="oneline"><div class="leftText">理由：</div><div class="rightText">'+dealStringUndefined(dict['AppReason'])+'</div></div>'
			if (Status =='已保存' || Status =='驳回') { //已驳回
				html = '<div class="mui-slider-right mui-disabled">'+
					'<div class="mui-btn mui-btn-red"><div class="deleteBox">'+
						'<div class="mui-icon mui-icon-trash"></div><div class="deleteText">删除</div></div>'+
						'<div class="seperateLine"></div></div>'+
					'</div><div class="mui-slider-handle">'+html+'</div>'
			}
			return html
		}
		
		var Status = dict['LeaveStatus']; // 状态 B：驳回 Y：提交 NA：护士长审核 ZA：科护士长审核 HA：护理部审核
		var statusIcon = 'nur-status'+Status
		if (Status == 'Y') { //已提交
			statusIcon = 'nur-statusR'
		}else if (source == 'N'&&Status.indexOf('A') != -1) { //护士请假--一层审批:护士长/科护士长/护理部谁审批都行
			statusIcon = 'nur-statusA'
		}else if (source == 'H' && Status == "HA") {
			statusIcon = 'nur-statusA'
		}else if (source == 'H' && Status == 'ZA') {
			statusIcon = 'nur-shenhezhong'
		}
		if (typeId != undefined) {
			statusIcon = ''
		}
		var typeDesc = dict['LeaveTypeDesc'].substring(0,1)
		var html = '<div class="liheader"><div class="headerImg shijia">'+typeDesc+'</div>'+
				'<div class="headerText mui-ellipsis">'+dict['CreatorName']+' '+pageType+'</div>'+
				'<div class="headerTime">'+dict['CreateDate']+'</div></div>'+
			'<div class="statusIcon"><div class="nurfont '+statusIcon+'"></div></div>'
		var leaveDate = dict['LeaveStDate']
		if (leaveDate != dict['LeaveEndDate']) {
			leaveDate = leaveDate +' - '+dict['LeaveEndDate']
		}
		var nameValue = dict['AuditorName'] //护士长审批
		var dateValue = dict['AuditDate']
		if (source == 'N') { //护士请假 一层审批即可
			if (nameValue == '') {
				nameValue = dict['ZAuditorName'] //科护士长审批
				dateValue = dict['ZAuditDate']
			}
			if (nameValue == '') {
				nameValue = dict['HAuditorName'] //护理部审批
				dateValue = dict['HAuditDate']
			}
			html += '<div class="oneline"><div class="leftText">请假类型：</div><div class="rightText">'+dict['LeaveTypeDesc']+'</div></div>'+
				'<div class="oneline"><div class="leftText">请假时间：</div><div class="rightText">'+leaveDate+'</div></div>'+
				'<div class="oneline"><div class="leftText">请假时长：</div><div class="rightText">'+dict['LeaveTotal']+'</div></div>'
		}else if (source == 'H') { //护士长请假备案 多层审批
			if (nameValue == '') {
				nameValue = dict['HAuditorName'] //护理部审批
				dateValue = dict['HAuditDate']
			}
			if (nameValue == '') {
				nameValue = dict['ZAuditorName'] //科护士长审批
				dateValue = dict['ZAuditDate']
			}
			html += '<div class="oneline"><div class="leftText">请假病区：</div><div class="rightText">'+dict['LeaveWardDesc']+'</div></div>'+
				'<div class="oneline"><div class="leftText">请假类型：</div><div class="rightText">'+dict['LeaveTypeDesc']+'</div></div>'+
				'<div class="oneline"><div class="leftText">请假时间：</div><div class="rightText">'+leaveDate+'</div></div>'+
				'<div class="oneline"><div class="leftText">请假事由：</div><div class="rightText">'+dict['LeaveReason']+'</div></div>'+
				'<div class="oneline"><div class="leftText">请假所在地：</div><div class="rightText">'+dict['LeavePosition']+'</div></div>'+
				'<div class="oneline"><div class="leftText">临时负责人：</div><div class="rightText">'+dict['LeaveCharger']+'</div></div>'+
				'<div class="oneline"><div class="leftText">临时负责人电话：</div><div class="rightText">'+dict['LeaveChargerTel']+'</div></div>'
		}
		
		html += '<div class="oneline"><div class="leftText">审核人：</div><div class="rightText">'+nameValue+'</div></div>'+
				'<div class="oneline"><div class="leftText">审核时间：</div><div class="rightText">'+dateValue+'</div></div>'
		if (typeId != undefined) {
			return html
		}
		if (source =="N" && Status == 'B') { //护士请假已驳回
			html = '<div class="mui-slider-right mui-disabled">'+
				'<div class="mui-btn mui-btn-red"><div class="deleteBox">'+
					'<div class="mui-icon mui-icon-trash"></div><div class="deleteText">删除</div></div>'+
					'<div class="seperateLine"></div></div>'+
				'</div><div class="mui-slider-handle">'+html+'</div>'
		}
		return html
	}
	//type P:排班 H:休假 S:特殊 意愿排班传P即
	function getPostData(isShowPopo){
		mui('#typeScroll .schItem').each(function (index,element) {
			element.removeEventListener('tap',schItemTapped)
		})
		var typeScroll = $('#typeScroll')[0]
		var pdict = {"start":0,'limit':"",'parr':'^'+wardID+'^H'} //P:排班 H:休假 S:特殊
		var paramDict = {'method':'web.INMDBComm:FindWardPostList','params':JSON.stringify(pdict),'type':'RecQuery'}
		//console.log(JSON.stringify(pdict))
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			if (result['msgcode'] != '1') {
				mui.toast(result['msg'])
				typeScroll.innerHTML = '<div class="schItem mui-ellipsis mui-active" data-index="-1" data-rw="">全部</div>'
				if (isShowPopo) {
					mui('#leaveType').popover('show')
				}
				return
			}
			var dataArr = result['data']
			postData = dataArr
			var html = '<div class="schItem mui-ellipsis mui-active" data-index="-1" data-rw="">全部</div>'
			for (var i = 0; i < dataArr.length; i++) {
				var oneData = dataArr[i]
				html +='<div class="schItem mui-ellipsis" data-index="'+i+'" data-rw="'+oneData['rw']+'">'+oneData['PostDesc']+'</div>'
			}
			typeScroll.innerHTML = html
			mui('#typeScroll .schItem').each(function (index,element) {
				element.addEventListener('tap',schItemTapped)
			})
			if (isShowPopo) {
				mui('#leaveType').popover('show')
			}
			
		}, function(errorStr) {
			typeScroll.innerHTML = '<div class="schItem mui-ellipsis mui-active" data-index="-1" data-rw="">全部</div>'
			if (isShowPopo) {
				mui('#leaveType').popover('show')
			}
		});
	}
	
	function schItemTapped(){
		var thisDom = this
		var dataIndex = thisDom.getAttribute('data-index')
		if (dataIndex == '-1') { //全部
			mui('#typeScroll .schItem.mui-active').each(function (index,element) {
				if (element != thisDom) {
					element.classList.remove('mui-active')
				}
			})
			thisDom.classList.add('mui-active')
			return
		}
		if (thisDom.getAttribute('class').indexOf('mui-active') != -1) {
			thisDom.classList.remove('mui-active')
			return
		}
		var quanbu = mui('#typeScroll .schItem.mui-active[data-index="-1"]')[0]
		if (quanbu) {
			quanbu.classList.remove('mui-active')
		}
		thisDom.classList.add('mui-active')
	}
	
	function baseSetup(){
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		var stBtn = $('#stBtn')[0]
		var endBtn = $('#endBtn')[0]
		var curMonth = getDeviceYearMonthStr();
		stBtn.innerText = curMonth +'-01'
		var currDict = getYMDWithStr(curMonth)
		endBtn.innerText = curMonth +'-' + getMonthMaxDay(currDict['year'], currDict['month'])
		stBtn.addEventListener('tap',dateBtnTapped)
		endBtn.addEventListener('tap',dateBtnTapped)
		var self = plus.webview.currentWebview()
		pageType = self.title
		typeId = self.typeId
		if (self.endDate) {
			stBtn.innerText = self.stDate
			endBtn.innerText = self.endDate
		}
		source="N"
		perInfo = app.getState()
		wardID = perInfo['loginWardId']
		method = 'web.INMLeaveComm:FindNurLeaveList'
		deleteMethod = 'web.INMLeaveComm:DeleteLeaveApp'
		switch (pageType){
			case '调岗申请':
				source=""
				method = 'web.INMPersonComm:FindTransPositionApp'
				deleteMethod = 'web.INMPersonComm:DeletePositionApp'
				break;
			case '护士长请假':
				source="H"
				if (typeId == undefined){
					$('.mui-bar')[0].classList.add('hasStatic')
				}
				break;
			default:
				if (typeId == undefined){
					$('.mui-bar')[0].classList.add('hasStatic')
				}
				break;
		}
		
		
		var scroll = document.getElementById('scroll');
		pullDom = $(scroll).pullToRefresh({
			up: {
				callback: function() {
					getApplyData()
				}
			}
		});
		if (typeId != undefined) {
			$('#addBtn')[0].style.display = 'none'
			$('.staticText')[0].innerText = self.staticText==undefined?"":self.staticText
		}else{
			$('.mui-title')[0].innerText = pageType
			getPostData();
		}
		
		getApplyData();
		//新建按钮
		mui('body').on('tap','#addBtn',function(){
			var pageName = "pmLeaveApplyAdd"
			if (pageType == '调岗申请') {
				pageName = 'pmTransPositionAdd'
			}
			var webview = plus.webview.create(pageName+'.html', pageName, {
				background: '#fff',
				statusbar: {
					background: '#fff'
				}
			},{'title':pageType});
			webview.show('fade-in');
		})
		mui('header').on('tap','.nur-shaixuan',function(){  //筛选
			if (postData) {
				mui('#leaveType').popover('show')
			}else{
				getPostData(true)
			}
		})
		mui('header').on('tap','.nur-jurassic_chart',function(){  //统计
			var pageName = "pmLeaveApplyStatics"
			var webview = plus.webview.create(pageName+'.html', pageName, {
				background: '#fff',
				statusbar: {
					background: '#fff'
				}
			},{'title':pageType});
			webview.show('fade-in');
		})
		//重置
		mui('#leaveType').on('tap','.resetBtn',function(){
			var quanbu = mui('#typeScroll .schItem[data-index="-1"]')[0]
			mui('#typeScroll .schItem.mui-active').each(function (index,element) {
				if (element != quanbu) {
					element.classList.remove('mui-active')
				}
			})
			quanbu.classList.add('mui-active')
		})
		//确定
		mui('#leaveType').on('tap','.sureBtn',function(){
			filterTypes = getFilterType()
			mui('#leaveType').popover('hide')
			resetRefresh();
		})
	}
	function getFilterType(){
		var types = ''
		mui('#typeScroll .schItem.mui-active').each(function (index,element) {
			if (index > 0) {
				types += '^'
			}
			types += element.getAttribute('data-rw')
		})
		return types;
	}
	function dateBtnTapped(){
		var _self = this;
		plus.nativeUI.pickDate(function(e) {
			var d = e.date;
			var dateStr = d.getFullYear()+'-'
			if (d.getMonth() < 9) {
				dateStr += '0'
			}
			dateStr += (d.getMonth() + 1)+'-'
			if (d.getDate() < 10) {
				dateStr += '0'
			}
			dateStr += d.getDate()
			_self.innerText = dateStr;
			resetRefresh();
		}, function(e) {}, {});
	}
	
	function dealStringUndefined(str){
		return str==undefined?"":str
	}
	var resetRefresh = function (){
		start = 0;
		sourceData = []
		mui('#UL li').each(function (index,element) {
			element.removeEventListener('tap',tableCellTapped)
		})
		 $('#UL')[0].innerHTML = '';
		 $('#wrapper').scroll().scrollTo(0,0,100);
		if (pullDom) {
			pullDom.refresh(true)
		}
		getApplyData();
	}
	var exportObj = {}
	exportObj.resetRefresh = resetRefresh
	return exportObj
})(mui)

function detailChanged(){
	listObj.resetRefresh();
}

/* 护士长请假备案
	护士长请假Y--科护士长审核ZA--护理部审核HA(审批结束)
	B状态驳回(审批结束)
	{"LeaveSource":"H","LeaveWard":"2","LeaveWardDesc":"内分泌科护理单元","LeaveType":"6","LeaveTypeDesc":"休假","LeaveStDate":"2022-05-31","LeaveStTime":"00:00",
	"LeaveEndDate":"2022-05-31","LeaveEndTime":"23:59","LeaveTotal":"1天","LeaveReason":"就是想请假","LeavePosition":"郑州","LeaveCharger":"小李",
	"LeaveChargerTel":"13262156210","LeaveStatus":"Y","LeaveReasonDesc":"提交","Creator":"31","CreatorName":"傅国宁","CreateDate":"2022-05-31",
	"CreateTime":"15:56:12","Auditor":"","AuditorName":"","AuditDate":"","AuditTime":"","ZAuditor":"","ZAuditorName":"","ZAuditDate":"",
	"ZAuditTime":"","HAuditor":"","HAuditorName":"","HAuditDate":"","HAuditTime":"","AuditOpinion":"","ZAuditOpinion":"","HAuditOpinion":"","rw":"8","WorkDate":""}]

请假申请
	护士请假Y---护士长审核NA(审批结束)
	{"LeaveSource":"N","LeaveWard":"2","LeaveWardDesc":"内分泌科护理单元","LeaveType":"6","LeaveTypeDesc":"休假","LeaveStDate":"2022-05-31","LeaveStTime":"00:00",
	"LeaveEndDate":"2022-05-31","LeaveEndTime":"23:59","LeaveTotal":"1天","LeaveReason":"","LeavePosition":"","LeaveCharger":"","LeaveChargerTel":"",
	"LeaveStatus":"NA","LeaveReasonDesc":"审核","Creator":"31","CreatorName":"傅国宁","CreateDate":"2022-05-31","CreateTime":"17:13:08","Auditor":"34",
	"AuditorName":"护士01","AuditDate":"2022-05-31","AuditTime":"18:51:59","ZAuditor":"","ZAuditorName":"","ZAuditDate":"","ZAuditTime":"","HAuditor":"",
	"HAuditorName":"","HAuditDate":"","HAuditTime":"","AuditOpinion":"批准喽","ZAuditOpinion":"","HAuditOpinion":"","rw":"9","WorkDate":""}


*/